import { Controller } from "../../mvc/core/Controller";
import { IPlayDiceModule } from "../../../modules/interfaces/IPlayDiceModule";
import { PlayDiceModule } from "../../../modules/PlayDiceModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { GameConst } from "../../component/gconst/GameConst";
import { PlayDiceHintButton } from "../view/dice/PlayDiceHintButton";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { Global } from "../../../com/gengine/global/Global";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SExpDice } from "../../../Message/Public/SExpDice";
type int = number;
//class PlayDiceController
    
    export  class PlayDiceController extends Controller
    {
       

        public get playDiceModule(): IPlayDiceModule
        {
            if (!this._playDiceModule) 
            {
                this._playDiceModule = new PlayDiceModule();
                this._playDiceModule.addEventListener(WindowEvent.SHOW, this.onPlayDiceWindowShow);
                this._playDiceModule.addEventListener(WindowEvent.CLOSE, this.onPlayDiceWindowClose);
            }
            return this._playDiceModule;
        }

        /* internal  */onPlayDiceWindowShow(arg1: WindowEvent): void
        {
            return;
        }

        /* internal  */onPlayDiceWindowClose(arg1: WindowEvent): void
        {
            this.hintBtnHandle();
            return;
        }

        /* internal  */hintBtnHandle(): void
        {
            if (cache.role.entityInfo.level >= GameConst.DicePlayerLevel) 
            {
                if (cache.playDice.fullTime) 
                {
                    if (cache.playDice.hasReward) 
                    {
                        this.playHintBtn.show();
                    }
                    else 
                    {
                        this.playHintBtn.hide();
                    }
                }
                else 
                {
                    this.playHintBtn.show();
                }
            }
            return;
        }

        public get playHintBtn(): PlayDiceHintButton
        {
            if (!this._playHintBtn) 
            {
                this._playHintBtn = new PlayDiceHintButton();
            }
            return this._playHintBtn;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicUpdateExpDice, this.onDiceChange);
            Dispatcher.addEventListener(EventName.DiceGoThrow, this.onDiceGoThrow);
            Dispatcher.addEventListener(EventName.ShowPlayDice, this.onShowPlayDice);
            Dispatcher.addEventListener(EventName.ClosePlayDice, this.onClosePlayDice);
            NetDispatcher.addCmdListener(ServerCommand.EntityInfo_Update, this.onLevelChange);
            return;
        }

        /* internal  */onClosePlayDice(arg1: DataEvent): void
        {
            this.playDiceModule.hide();
            this.playHintBtn.hide();
            return;
        }

        /* internal  */onLevelChange(arg1: Object=null): void
        {
            this.hintBtnHandle();
            return;
        }

        /* internal  */onShowPlayDice(arg1: DataEvent): void
        {
            if (Global.isDebugModle) 
            {
                this.showPlayDice();
            }
            else if (cache.playDice.sExpDice) 
            {
                if (cache.role.entityInfo.level >= GameConst.DicePlayerLevel) 
                {
                    if (cache.playDice.fullTime) 
                    {
                        if (cache.playDice.hasReward) 
                        {
                            this.showPlayDice();
                        }
                        else if (!cache.playDice.hasReward) 
                        {
                            MsgManager.showRollTipsMsg(Language.getString(70303));
                            return;
                        }
                    }
                    else 
                    {
                        this.showPlayDice();
                    }
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(70302));
                    return;
                }
            }
            return;
        }

        public showPlayDice(): void
        {
            if (this.playDiceModule.isHide) 
            {
                this.playDiceModule.show();
                this.playDiceModule.updateInfo(cache.playDice.sExpDice, -1);
            }
            else 
            {
                this.playDiceModule.hide();
            }
            return;
        }

        /* internal  */onDiceGoThrow(arg1: DataEvent): void
        {
            var loc1=arg1.data.type as int;
            var loc2=arg1.data.bool as Boolean;
            GameProxy.roleProxy.throwExpDice(loc1, loc2);
            return;
        }

        /* internal  */onDiceChange(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SExpDice;
            cache.playDice.sExpDice = loc1;
            if (Global.isDebugModle) 
            {
                if (cache.role.entityInfo.level < GameConst.DicePlayerLevel) 
                {
                    this.playHintBtn.hide();
                }
                else if (cache.role.entityInfo.level >= GameConst.DicePlayerLevel) 
                {
                    this.playHintBtn.show();
                }
            }
            else 
            {
                this.hintBtnHandle();
            }
            if (this._playDiceModule && !(cache.playDice.type == 2)) 
            {
                this._playDiceModule.updateInfo(loc1, cache.playDice.type);
            }
            return;
        }

        private /* var */_playDiceModule: IPlayDiceModule;

        private /* var */_playHintBtn: PlayDiceHintButton;
    }
